#ifndef _TESTLIB
#define _TESTLIB

#include "funct.h"
#include <stdio.h>

void printTree(TNode *t){
	double fr;
	long lval;
	
	if(t==NULL)
		return;
		
	switch(t->function){
		case F_COE:
			lval = l_cast(t->value, &fr);
			if(fr != 0.0)
				printf("%lf", t->value);
			else
				printf("%ld", lval);
		break;
		
		case F_VAR:
			printf("%c", t->chr);
		break;
		
		case F_OPT:
			/*if( (t->parent != NULL) && (t->parent)->priority < t->priority)*/
				printf("(");
			if(t->left != NULL)
				printTree(t->left);
			printf("%c", t->chr);
			if(t->right != NULL)
				printTree(t->right);
			/*if( (t->parent != NULL) && (t->parent)->priority < t->priority)*/
				printf(")");
		break;
		
		case F_FUNCT:
			if(t->sign == -1)
				printf("-");
				
			switch(t->chr){
				case SIN:
					printf("sin(");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case ASIN:
					printf("asin( ");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case COS:
					printf("cos(");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case TAN:
					printf("tan(");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case ACOS:
					printf("acos( ");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case ATAN:
					printf("atan( ");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case SEC:
					printf("sec( ");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case LN:
					printf("ln(");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case LOG:
					printf(" log(");
					if(t->left != NULL)
						printTree(t->left);
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case POW:
					printf("(");
					if(t->left != NULL)
						printTree(t->left);
					printf("^");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case SQRT:
					printf("sqrt(");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
			}
		break;
	}
}

void toString(TNode *t, char *str, int *curpos, int len){
	double fr;
	long lval;
	int i, l;
	char tmp[16];
	
	if(t==NULL)
		return;
		
	switch(t->function){
		case F_COE:
			lval = l_cast(t->value, &fr);
			if(fr != 0.0){
				l = sprintf(tmp, "%lf", t->value);
			}
			else{
				l = sprintf(tmp, "%ld", lval);
			}
			for(i=0; i<l; i++){
				str[*curpos] = tmp[i];
				(*curpos)++;
			}
		break;
		
		case F_VAR:
			l = sprintf(tmp, "%c", t->chr);
			for(i=0; i<l; i++){
				str[*curpos] = tmp[i];
				(*curpos)++;
			}
		break;
		
		case F_OPT:
			/*if( (t->parent != NULL) && (t->parent)->priority < t->priority)*/
			printf("(");
			if(t->left != NULL)
				printTree(t->left);
			printf("%c", t->chr);
			if(t->right != NULL)
				printTree(t->right);
			/*if( (t->parent != NULL) && (t->parent)->priority < t->priority)*/
				printf(")");
		break;
		
		case F_FUNCT:
			if(t->sign == -1)
				printf("-");
				
			switch(t->chr){
				case SIN:
					printf("sin(");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case ASIN:
					printf(" asin ");
				break;
				
				case COS:
					printf("cos(");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case ACOS:
					printf(" acos ");
				break;
				
				case LN:
					printf(" ln(");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case LOG:
					printf(" log(");
					if(t->left != NULL)
						printTree(t->left);
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
				
				case POW:
					printf("(");
					if(t->left != NULL)
						printTree(t->left);
					printf("^");
					if(t->right != NULL)
						printTree(t->right);
					printf(")");
				break;
			}
		break;
	}
}
#endif
